package com.peng.test.jdk;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import org.junit.Test;

/**
 * LocalDateTimeSample
 * <p>
 * Created on 2022/11/29
 *
 * @author lupeng
 */
public class LocalDateTimeSample {

    @Test
    public void t1() {
        // LocalTime start = LocalTime.parse("13:00:00");
        LocalTime start = LocalTime.parse("17:00:00");
        LocalTime end = LocalTime.parse("18:00:00");
        LocalTime now = LocalTime.now();
        System.out.println(start.isBefore(now) && end.isAfter(now));
    }

    @Test
    public void t2() {
        System.out.println(LocalDate.now());
        LocalDateTime dateTime = LocalDateTime.of(LocalDate.now(), LocalTime.now());
        System.out.println(dateTime);
        System.out.println(dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    }

    /**
     * LocalDateTime 和时间戳之间的相互转换
     *
     * @author lupeng6
     * @date 2023/3/26 21:11
     */
    @Test
    public void t3() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        System.out.println(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(1669728326199L), ZoneId.systemDefault());
        System.out.println(formatter.format(localDateTime));


        System.out.println(DateTimeUtils.localDateTime2ts(LocalDateTime.now()));
        System.out.println(formatter
                .format(DateTimeUtils.ts2LocalDateTime(new Date().getTime())));
    }

    @Test
    public void t4() {
        System.out.println(LocalDateTime.now().toInstant(ZoneOffset.of("+0")).toEpochMilli());
    }

    /**
     * 向前向后推移时间
     *
     * @author lupeng6
     * @date 2023/3/26 21:20
     */
    @Test
    public void t5() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime now1 = LocalDateTime.now().plusDays(1);
        System.out.println(formatter.format(now1));

        LocalDateTime now2 = LocalDateTime.now().minusDays(2);
        System.out.println(formatter.format(now2));

        LocalDateTime now = LocalDateTime.now();
        // 判断当前时间是否在某个时间范围
        System.out.println(now.isBefore(now1) && now.isAfter(now2));
    }


    @Test
    public void t6() {
        LocalDateTime now = LocalDateTime.now();

    }

}
